package user

import (
	"crypto/sha256"
	"fmt"
	"github.com/go-pay/gopay/pkg/jwt"
	"math/rand"
	"regexp"
	"strings"
	"time"
)

// 密码加密方法
func PasswordEncryption(pwd string) string {
	hash := sha256.Sum256([]byte(pwd))
	encryptedPwd := fmt.Sprintf("%x", hash[:])[:32]
	return encryptedPwd
}
func CheckPhone(phone string) bool {
	// 定义正则表达式
	regex := `^1[3-9]\d{9}$`
	// 编译正则表达式
	re := regexp.MustCompile(regex)
	return re.MatchString(phone)

}

// 阿里云发送短信
func SendSms(phone_number string, sms_content string, accessKeyid string, accessKeySrcret string, SignName string, TemplateCode string) (bool, error) {
	//client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", accessKeyid, accessKeySrcret)
	//if err != nil {
	//	return false, err
	//}
	//request := requests.NewCommonRequest()
	//request.Method = "POST"
	//request.Scheme = "https"
	//request.Domain = "dysmsapi.aliyuncs.com"
	//request.Version = "2017-05-25"
	//request.ApiName = "SendSms"
	//request.QueryParams["PhoneNumbers"] = phone_number
	//request.QueryParams["SignName"] = SignName
	//request.QueryParams["TemplateCode"] = TemplateCode
	//request.QueryParams["TemplateParam"] = sms_content
	//response, err := client.ProcessCommonRequest(request)
	//if err != nil {
	//	fmt.Println(err)
	//	return false, err
	//}
	//
	//fmt.Println(string(response.GetHttpContentBytes()))
	fmt.Println("手机验证码", sms_content)
	return true, nil
}

// 密钥用于签名和验证 JWT
var jwtKey = []byte("8d969eef6ecad3c29a3a629280e686cf")

type Claims struct {
	Id string `json:"id"`
	jwt.StandardClaims
}

// 获取Token
func (c *Claims) GenerateJWT(id string) (string, error) {
	// 设置过期时间为 24 小时后
	expirationTime := time.Now().Add(24 * time.Hour)
	claims := &Claims{
		Id: id,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: expirationTime.Unix(),
		},
	}
	// 创建一个新的 JWT 对象，使用 HMAC SHA256 算法签名
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	// 使用密钥签名并返回生成的 JWT 字符串
	return token.SignedString(jwtKey)
}

// 验证 JWT 函数
func (c *Claims) ValidateJWT(tokenString string) (*Claims, error) {
	// 解析 token
	claims := &Claims{}
	token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
		// 验证签名方法是否为 HMAC SHA256
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("签名方法错误: %v", token.Method.Alg())
		}
		return jwtKey, nil
	})
	if err != nil {
		return nil, err
	}
	// 验证 token 是否有效
	if !token.Valid {
		return nil, fmt.Errorf("token无效")
	}

	return claims, nil
}

// 预定义的文艺形容词列表
var elegantAdjectives = []string{
	"空灵", "深邃", "绮丽", "静谧", "璀璨",
	"隽永", "逸尘", "灵犀", "幽婉", "玄秘",
}

// 预定义的文艺名词列表
var elegantNouns = []string{
	"星云", "幽梦", "弦歌", "流萤", "墨韵",
	"灵羽", "韶光", "曦月", "澜波", "幻蝶",
}

// 预定义的英文单词列表
var englishWords = []string{
	"Dreamer", "Poet", "Wanderer", "Enchanter", "Sage",
}

// GenerateNickname 函数用于随机生成高级昵称，不包含随机数
func GenerateNickname() string {
	rand.Seed(time.Now().UnixNano())
	// 随机选择一个形容词
	adjIndex := rand.Intn(len(elegantAdjectives))
	adjective := elegantAdjectives[adjIndex]
	// 随机选择一个名词
	nounIndex := rand.Intn(len(elegantNouns))
	noun := elegantNouns[nounIndex]
	// 有一定概率添加英文单词
	addEnglish := rand.Intn(2) == 0
	var englishPart string
	if addEnglish {
		wordIndex := rand.Intn(len(englishWords))
		englishPart = englishWords[wordIndex]
	}
	// 组合各部分形成昵称
	parts := []string{adjective, noun, englishPart}
	filteredParts := make([]string, 0)
	for _, part := range parts {
		if part != "" {
			filteredParts = append(filteredParts, part)
		}
	}
	return strings.Join(filteredParts, "")
}

func ProfilePicture() string {
	imageURLs := []string{
		"https://i.hd-r.icu/616af792d69706422edc66e687fef016.png",
		"https://i.hd-r.icu/22cd9211e007e64f8b7b332e935c12fe.png",
		"https://i.hd-r.icu/78ead98ffe9666795b7b6e3b5e56ab48.png",
		"https://i.hd-r.icu/233475bcc701b7de96cd12b24d47d23c.png",
		"https://i.hd-r.icu/cade5db10908ee457e976f18e6e5f96e.png",
		"https://i.hd-r.icu/7dda37eafa0b9e38f111d061fad65aad.png",
		"https://i.hd-r.icu/3b4c8f308d485104bcbadc5b30dd8e98.png",
		"https://i.hd-r.icu/b23555fc53b85369beb14136d49a7108.png",
		"https://i.hd-r.icu/be190aa702986463e17f51cb1b24f850.png",
		"https://i.hd-r.icu/b66733d22a681e929ebc2f4287fafc32.png",
		"https://i.hd-r.icu/433b6e16528d44baa13caa01c397e95a.png",
		"https://i.hd-r.icu/9c90424464c93af299619b9012bd8ecc.png",
		"https://i.hd-r.icu/8cb0a76c8eb20d8955e46910abce204e.png",
		"https://i.hd-r.icu/48773488e5d63c3f8b9b1749b28196ee.png",
	}
	// 初始化随机数种子
	rand.Seed(time.Now().UnixNano())
	// 生成随机索引
	randomIndex := rand.Intn(len(imageURLs))
	// 根据随机索引选择图片地址
	randomImageURL := imageURLs[randomIndex]
	return randomImageURL
}
